package taller2.view.windows.CreatorWindows;


import taller2.interfaces.AlgorithmApplicator;
import taller2.interfaces.ChangeableWindow;
import taller2.interfaces.Graph;
import taller2.model.factorys.GraphFactory;
import taller2.utils.Point;
import taller2.view.draws.DrawingArea;
import taller2.view.menus.CreatorMenus.GraphCreatorMenu;
import taller2.view.menus.CreatorMenus.StronglyConnectedCreatorMenu;


public class StronglyConnectedCreatorWindow extends GraphCreatorWindow {

	private boolean load;
	
	public StronglyConnectedCreatorWindow(ChangeableWindow window, Graph mainGraph, DrawingArea area,
			AlgorithmApplicator algorithmApplicator) {
		super(window, mainGraph, area, algorithmApplicator);		
	}
	
	@Override
	protected GraphCreatorMenu createGraphCreatorMenu(){
		return (new StronglyConnectedCreatorMenu(this.localArea,this.localGraph, this, this.algorithmApplicator));
	}
	
	@Override
	public Graph load(String path) {
		load = true;
		
		localGraph.load(path);
		
		if (localGraph.savedAsDirected()){
			localGraph = GraphFactory.directedGgraphInstance();
			localGraph.load(path);
		}else{
			localGraph = GraphFactory.undirectedGraphInstance();
			localGraph.load(path);
		}
		
		//porque tengo que darle el grafo nuevo
		this.drawer.setGraph(localGraph);
		
		this.window.remove(selector);
		this.selector = createGraphCreatorMenu();
		this.window.addComponent(selector, 1, 0, new Point(0, 1));
		
		this.window.redraw();
		
		return localGraph;
	}
	
	@Override
	public void applyChanges(){
		
		if (load){
			mainGraph.randomGraph(0, 0);
			mWindow.aplicarCambios(localGraph);
		}
		else{		
			mainGraph.replace(localGraph);
		}
		
		window.setVisible(false);
		window.dispose();
		area.getDrawer().loadSelection();
		area.repaint();
		
		if(algorithmApplicator != null){
			algorithmApplicator.reset();
			
			if (load)
				algorithmApplicator.solveProblem(localGraph);
			else
				algorithmApplicator.solveProblem(mainGraph);
		}
		
		load = false;
	}
}